﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace studentmanagementdemo
{
    public partial class frmsinhvienthem : Form
    {

        private SqlConnection con;
        private DataTable dt = new DataTable("tblStudent");
        private DataTable dtClass = new DataTable("tblClass");
        private SqlDataAdapter da = new SqlDataAdapter();
        private Boolean kt;

        private void connect()
        {
            //String cn = "Data Source=127.0.0.1;Initial Catalog=iPMAC;Persist Security Info=True;User ID=sa;Password=sa";
            String cn = "Data Source=(local);Initial Catalog=iPMAC;Integrated Security=True";
            try
            {
                con = new SqlConnection(cn);
                con.Open(); //Moi ket noi                
            }
            catch (Exception ex)
            {
                MessageBox.Show("Không thể kết nối tới cơ sở dữ liệu !", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void disconnect()
        {
            con.Close(); //Dong ket noi
            con.Dispose(); //Giai phong tai nguyen
            con = null; //Huy doi tuong
        }

        private void getdata()
        {
            SqlCommand command = new SqlCommand(); //Khai bao 1 command
            command.Connection = con; //Ket noi
            command.CommandType = CommandType.Text; //Khai bao kieu command
            command.CommandText = "Select * from tblStudent"; //Cau truy van SQL
            da.SelectCommand = command; //Gan command cho da            
            da.Fill(dt); //Nap du lieu vao table            

            command.CommandText = "Select * From tblClass";
            da.SelectCommand = command;
            da.Fill(dtClass);
            cbolop.DataSource = dtClass;
            cbolop.DisplayMember = "fldClassName";
            cbolop.ValueMember = "fldID";
        }

        public frmsinhvienthem()
        {
            InitializeComponent();
        }

        private void frmsinhvienthem_Load(object sender, EventArgs e)
        {
            connect();
            getdata();
            dtngay.Value = DateTime.Now;
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            Close();
            Dispose();
            disconnect();
            frmsinhvien _frmsinhvien = new frmsinhvien();
            _frmsinhvien.Show();
        }

        private bool kiemtra()
        {
            kt = true;
            if (txthosv.Text == "")
            {
                MessageBox.Show("Bạn chưa nhập họ của SV !", "THÔNG BÁO", MessageBoxButtons.OK);
                kt = false;
            }
            else if (dtngay.Value == null)
            {
                MessageBox.Show("Bạn chưa ngày tháng !", "THÔNG BÁO", MessageBoxButtons.OK);
                kt = false;
            }
            return kt;
        }

        private void btnluu_Click(object sender, EventArgs e)
        {
            kiemtra();
            if (kt == true)
            {
                DataRow row = dt.NewRow();
                row["fldFirstName"] = txthosv.Text;
                row["fldLastName"] = txttensv.Text;
                row["fldAge"] = txttuoi.Text;
                if (cbogioitinh.Text == "Nam")
                    row["fldSex"] = 1;
                else if (cbogioitinh.Text == "Nữ")
                    row["fldSex"] = 0;
                row["fldCreatedDate"] = dtngay.Value;
                row["fldClassID"] = cbolop.SelectedValue;
                dt.Rows.Add(row);
                SqlCommand commandInsert = new SqlCommand();
                commandInsert.Connection = con;
                commandInsert.CommandType = CommandType.Text;
                commandInsert.CommandText = @"Insert tblStudent (fldFirstName, fldLastName, fldAge, fldSex, fldCreatedDate, fldClassID) 
                                                       Values(@fldFirstName, @fldLastName, @fldAge, @fldSex,@fldCreatedDate, @fldClassID)";
                commandInsert.Parameters.Add("@fldFirstName", SqlDbType.NVarChar, 50, "fldFirstName");
                commandInsert.Parameters.Add("@fldLastName", SqlDbType.NVarChar, 50, "fldLastName");
                commandInsert.Parameters.Add("@fldAge", SqlDbType.Int, 50, "fldAge");
                commandInsert.Parameters.Add("@fldSex", SqlDbType.Int, 50, "fldSex");
                commandInsert.Parameters.Add("@fldCreatedDate", SqlDbType.DateTime, 50, "fldCreatedDate");
                commandInsert.Parameters.Add("@fldClassID", SqlDbType.Int, 50, "fldClassID");
                da.InsertCommand = commandInsert;
                da.Update(dt);
                MessageBox.Show("Bạn đã thêm thành công !", "THÔNG BÁO", MessageBoxButtons.OK);
                Close();
                disconnect();
                Dispose();
                frmsinhvien _frmsinhvien = new frmsinhvien();
                _frmsinhvien.Show();
            }
        }

        private void btnhuy_Click(object sender, EventArgs e)
        {
            txthosv.Text = "";
            txttensv.Text = "";
            txttuoi.Text = "";
            cbogioitinh.Text = "";
            cbolop.Text = "";
            dtngay.Value = DateTime.Now;
        }
    }
}
